window.onload= function(){
    if(localStorage.username){
        $("#username").val(localStorage.username);

    }
    if(localStorage.password){
        $("#password").val(localStorage.password);

    }




//保存登录的所有用户
    var users = [
        {"username":'admin',"password":'admin',"touxiang":"./images/touxiang2.jpg"},
        {"username":'张三',"password":'123456',"touxiang":"./images/touxiang1.jpg"},
    ];
    //登录模块
    $("#submit").on("click",function(){

        var username =  $("#username").val();
        var password =  $("#password").val();
//判断用户名是否为空
        if(username === "Username" || password === "Password"){//用户名或密码为空
            $("#message").css("display","block");
        }else {
            if(users){//如果users数组里有数据
                var flag = false;//标识用户是否存在
                users.forEach(function(user,index){

                    if(user.username === username){
                        flag = true;
                        if(user.password === password){
                            //登录成功
                            //记住用户名
                            localStorage.username = username;
                            sessionStorage.username = username;
                            //记住密码处理模块
                            if($("#checkPassword")[0].checked){

                                localStorage.password = password;
                                sessionStorage.password = password;
                                localStorage.img = "./images/touxiang2.jpg";
                                sessionStorage.img = "./images/touxiang2.jpg";
                                localStorage.userId = index;//保存当前登录用户的id，以数组的索引为id
                                sessionStorage.userId = index;//保存当前登录用户的id,发表留言时使用
                                var user = {};//保存单个用户
                                user.username = username;
                                user.password = password;
                                users.push(user);
                            }

                            //跳转到首页
                            location.href = './index2.html';


                        }else{
                            $("#message").html("密码错误!").css("display","block");
                        }
                    }
                    // else{
                    //    $("#message").html("用户名不存在，请先注册!").css("display","block");
                    //}
                })

                if(!flag){
                    $("#message").html("用户名不存在，请先注册!").css("display","block");
                }

            }else{
                $("#message").html("用户名不存在，请先注册!").css("display","block");
            }

        }
    })


}



